<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<%@page import="com.yc.comm.Constant"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath(); 
String strUserId = request.getSession().getAttribute("stxtUName").toString();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<style type="text/css" media="all">   
	    .allow-float {clear:none!important;} /* 允许该元素浮动 */  
	    .stop-float {clear:both!important;} /* 阻止该元素浮动 */  
	   	.icon-first {float:left;}   
	   	.icon-choosed {float:left;padding:5 0 0 5px;}
	    .icon-second {float:left;padding:0 0 0 20px;} 
	    .win-first {float:left;}   
	    .win-second {float:left;padding:0 0 0 20px;}   
	   	.standby-yes {float:left;}   
	    .standby-no {float:left;padding:0 0 0 20px;}   
	    .age-field {float:left;padding:0 0 0 58px;*padding:0 0 0 50px!important;*padding:0 0 0 50px;}   
	</style> 
  
    <base href="<%=basePath%>"> 
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script> 
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type="text/javascript" src="<%=path%>/js/ext/ext-lang-zh_CN.js"></script>
	<script type='text/javascript' src='/btsm/dwr/interface/JSystemWeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 
  </head>
<body> 	 
	<div id="userContent" style="width:100%;height:100%"/>   
<script type="text/javascript">

var ds; 

var loginDisabledData = [['<%=Constant.DISABLE_USER%>', '禁止'],['<%=Constant.ENABLE_USER%>', '允许']];
	
var loginDisabledStore = new Ext.data.SimpleStore({
	fields: ['id', 'text'],
	data : loginDisabledData
});

var loginDisabledCombo = new Ext.form.ComboBox({	   
	fieldLabel: '能否登录',
	name: 'logindisabled',	
	width:200,
	valueField :"id",
	displayField: "text",
	mode: 'local',
	selectOnFocus:true,                    
	forceSelection: true,
	blankText:'请选是否允许该用户登录',
	emptyText:'请选是否允许该用户登录',
	hiddenName:'logindisabled',
	triggerAction: 'all',
	allowBlank:false,
	editable:false, 
	store: loginDisabledStore,
	value:0
});

var loginDisabledEditCombo = new Ext.form.ComboBox({	   
	fieldLabel: '能否登录',
	name: 'logindisabled',	
	width:200,
	valueField :"id",
	displayField: "text",
	mode: 'local',
	selectOnFocus:true,                    
	forceSelection: true,
	blankText:'请选是否允许该用户登录',
	emptyText:'请选是否允许该用户登录',
	hiddenName:'logindisabled',
	triggerAction: 'all',
	allowBlank:false,
	editable:false, 
	store: loginDisabledStore,
	value:0
});

 
var roleCombo = new Ext.form.ComboBox({		
		fieldLabel: '用户角色',
		name: 'rights',
		valueField :"id",
		displayField: "text",
		loadingText:'正从后台读取数据..',	
		mode: 'local',
		width:200,
		selectOnFocus:true,                    
		forceSelection: true,
		hiddenName:'rights',
		triggerAction: 'all',
		editable:false, allowBlank:false,
		blankText :'请选择用户角色',
		store: new Ext.data.SimpleStore({
			fields: ["id", "text"],
			data: []
		}) 
	});
	
var roleEditCombo = new Ext.form.ComboBox({		
		fieldLabel: '用户角色',
		name: 'rights',
		valueField :"id",
		displayField: "text",
		loadingText:'正从后台读取数据..',	
		mode: 'local',
		width:200,
		selectOnFocus:true,                    
		forceSelection: true,
		hiddenName:'rights',
		triggerAction: 'all',
		editable:false, allowBlank:false,
		blankText :'请选择用户角色',
		store: new Ext.data.SimpleStore({
			fields: ["id", "text"],
			data: []
		}) 
	});	
	
	
JSystemWeb.getComboArrayForRoles(function(returnData)
{
	var data = []; 
	for (var i = 0; i < returnData.length; i++) { 
		data.push([returnData[i][0], returnData[i][1]]);
	}
	roleCombo.store.loadData(data);
	roleEditCombo.store.loadData(data);
})
	

Ext.onReady(function(){
	Ext.QuickTips.init(); 
	Ext.form.Field.prototype.msgTarget = 'side'; 
	var viewport = new Ext.Viewport({
		layout:'border',
		frame:true,
		items:[ 
			{
				region: 'center',
				contentEl: 'userContent',
				margins:'110 30 30 30',
				split: true,
				border: true	
			}]
		});

	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'username', type: "string"},
							   {name: 'truename', type: "string"},
							   {name: 'rights', type: "string"},
							   {name: 'skin', type: "string"},
							   {name: 'identity', type: "string"},
							   {name: 'telephone', type: "string"},
							   {name: 'logindisabled', type: "string"},
							   {name: 'refreshtime', type: 'date', dateFormat: 'Y/m/d'},
							   {name: 'address',  type: 'string'}
						 ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,
				//{header: "ID", width: 50, sortable: true, dataIndex: 'id',align:'left'},	            
				{header: "用户ID", width: 80, sortable: true, dataIndex: 'username',align:'left'},
				{header: "用户名", width: 140, sortable: true, dataIndex: 'truename',align:'left'},
				{header: "身份证编号", width: 140, sortable: true, dataIndex: 'identity',align:'left'},	
				{header: "电话号码", width: 140, sortable: true, dataIndex: 'telephone',align:'left'},	
				{header: "能否登录", width: 140, sortable: true, dataIndex: 'logindisabled',align:'left',renderer: change},	
				{header: "更新时间", width: 140, sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d'),dataIndex: 'refreshtime',align:'left'},	
				{header: "地址", width: 140, sortable: true, dataIndex: 'address',align:'left'},
				{header: "操作", width: 100, sortable: false, dataIndex: 'id',renderer: showOperation},				
				{header: "删除", width: 50, sortable: false, dataIndex: 'id',renderer: showDelete}				
			]
		);
	function change(val)
	{
		if(val == 0){
			return '<span style="color:red;">No</span>';
		}else if(val == 1){
			return '<span style="color:green;">Yes</span>';
		}
		return val;
	}	
		
	function showOperation(value, cellmeta, record, rowIndex, columnIndex, store)
	{
		var logindisabled = record.get("logindisabled");
		if(logindisabled == "0")
			return "<input type='button' value='允许' class='x-btn button' onclick='doEnableUser(\"" + value  +   "\")'>"; 
		else if(logindisabled == "1")
			return "<input type='button' value='禁止' class='x-btn button' onclick='doDisableUser(\"" + value  +   "\")'>"; 	
	}	
		
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doDeleteFiberLink(\"" + id  +   "\")'>";  
	}

	ds = new Ext.data.Store({
			proxy: new Ext.data.DWRProxy(JSystemWeb.getUserList, true),	
			reader: new Ext.data.ListRangeReader({
				totalProperty: 'totalSize',
				id: 'id'
			}, recordType),
			sortInfo:{field:'id', direction:'ASC'}// 排序信息
		});
		
	var btnAddUser = new  Ext.Button({
					text:'创建用户',
					pressed:true,					 
					handler:createUser
				});
	var btnEditUser = new  Ext.Button({
					text:'修改用户信息',
					pressed:true,					 
					handler:editUser
				});	

	var btnEditPassword = new  Ext.Button({
					text:'修改密码',
					pressed:true,					 
					handler:editPassword
				});					
		
	var contentDiv = Ext.get("userContent");
	var grid = new Ext.grid.GridPanel({
	    	el: 'userContent',	    	
	        ds: ds,
	        cm: cm,
			sm:sm,
			title: '用户列表',
		    width: contentDiv.getComputedWidth(),
			height: contentDiv.getComputedHeight(), 
	        frame:true,
	        collapsible:false, 
	        enableDragDrop:false, 
			tbar: [
				btnAddUser,
				'-',
				btnEditUser,
				'-',
				btnEditPassword
				],
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: ds,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })
					
				
	    });
	    
	
    grid.render(); 
	ds.load({params:{start:0,limit:17}});

	function createUser()
	{
		//var data = {'id':"",
		//			'username':"",
			//		'passwd':"",
			//		'rights':"",
				///	'truename':"",
				//	'identity':"",
				//	'telephone':"",
				//	'address':"",
				//	'logindisabled':""
				//	};
		var data = {};			
		var record = new Ext.data.Record(data);
		userWindow.show();
		userWindow.formPanel.getForm().loadRecord(record);		
	}
	
	function editUser()
	{
		var record = grid.getSelectionModel().getSelected();
		if(!record)
		{
			Ext.Msg.alert("提示","请先选择需要修改的用户 !");
			return;
		}
		
		JSystemWeb.getRoleByUserID(record.get("id"),function(data)
		{
			//alert(data);
			record.set("rights",data);
			userEditWindow.show();
			userEditWindow.formPanel.getForm().loadRecord(record);
		}) 
		
		
	}
	function editPassword()
	{
		var record = grid.getSelectionModel().getSelected();
		if(!record)
		{
			Ext.Msg.alert("提示","请先选择需要修改的用户 !");
			return;
		}
		passwordWindow.show();
		passwordWindow.formPanel.getForm().loadRecord(record);
	}
	
	 
	var userForm = new Ext.FormPanel({
	    	labelAlign: 'right',
	        frame:true,	        
	        labelWidth:80,	       
			anchor:'90%',
			height:350,		
			buttonAlign:'right', 
			border: false,
			layout:'column',
			margins:'35 35 35 35',	
	        items: [ 
				{
					columnWidth:.5,
					layout: 'form',  
					labelWidth:70,	 
					items: [
							{
							xtype : 'hidden',
							fieldLabel : "id",
							readOnly : true,
							name : "id"
						},
						{
							xtype:'textfield',
							fieldLabel: '用户ID ',
							name: 'username',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入用户ID'
						},
						{
							xtype:'textfield',
							inputType: 'password' ,
							fieldLabel: '密码 ',
							name: 'passwd',
							width:200,  
							allowBlank:false,
							blankText :'请输入密码'
						},
						{
							xtype:'numberfield',
							fieldLabel: '身份证',
							name: 'identity',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入身份证'
						},
						roleCombo,
						loginDisabledCombo
					]
				},
				{
					columnWidth:.5,
					layout: 'form',  
					labelWidth:70,	 
					items: [
						{
							xtype:'textfield',
							fieldLabel: '用户名 ',
							name: 'truename',
							width:200,  
							allowBlank:false,
							blankText :'请输入用户名'
						}, 
						{
							xtype:'textfield',
							inputType: 'password' ,
							fieldLabel: '密码确认 ',
							name: 'passwdTwo',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入确认密码'
						},
						{
							xtype:'numberfield',
							fieldLabel: '电话',
							name: 'telephone',
							width:200,  
							allowBlank:false,
							blankText :'请输入电话号码'
						},
						{
							xtype:'textfield',
							fieldLabel: '地址',
							name: 'address',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入地址信息'
						}
					]
				}
				]
		});		
	var UserWindow = function() {
		var _win = this;
		this.formPanel = userForm;
		var _form = this.formPanel.getForm();
		UserWindow.superclass.constructor.call(this, { 
			title: '用户信息',
			width : 650,
			height : 230,
			resizable : true,
			plain : false,
			border : false,
			modal : true,
			autoScroll : false,
			layout : 'fit',
			closeAction : 'hide',
			items : this.formPanel,
			buttonAlign:'center',
			buttons : 
			[
				{	text : ' 保存',
					handler : function() 
					{
						if (_form.isValid()) 
						{
							var form = _form.getValues(false);
							if(form.passwd != form.passwdTwo)
							{
								Ext.Msg.alert("提示","确认密码与第一次密码不一致!");
								return;
							}
							UserBO = {};
							UserBO.id = form.id;	
							UserBO.username = form.username;	
							UserBO.passwd = form.passwd;	
							UserBO.rights = form.rights;	
							UserBO.truename = form.truename;	
							UserBO.identity = form.identity;	
							UserBO.telephone = form.telephone;	
							UserBO.address = form.address;	
							UserBO.logindisabled = form.logindisabled; 
							JSystemWeb.createUser(UserBO,function(data)
							{
								if(data)
	          					{
	          						_win.hide();
	          						Ext.Msg.alert('成功',"操作成功！",function()
	          						{
										ds.load({params:{start:0,limit:17}});
	          						}); 
									_form.reset(); 
	          					}else
	          					{
	          						Ext.Msg.alert("失败","操作失败!");
	          					}																
							})							
							
						}
					}
				},{
					text : '取消',
					handler : function() {
						_win.hide();
					},
					tooltip : '关闭对话框'
				}
			]	
		});
	}
	Ext.extend(UserWindow, Ext.Window); 
	var userWindow = new UserWindow();	
	
	
	var userEditForm = new Ext.FormPanel({
	    	labelAlign: 'right',
	        frame:true,	        
	        labelWidth:80,	       
			anchor:'90%',
			height:350,		
			buttonAlign:'right', 
			border: false,
			layout:'column',
			margins:'35 35 35 35',	
	        items: [ 
				{
					columnWidth:.5,
					layout: 'form',  
					labelWidth:70,	 
					items: [
							{
							xtype : 'hidden',
							fieldLabel : "id",
							readOnly : true,
							name : "id"
						},
						{
							xtype:'textfield',
							fieldLabel: '用户ID ',
							name: 'username',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入用户ID'
						},
						{
							xtype:'numberfield',
							fieldLabel: '身份证',
							name: 'identity',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入身份证'
						},
						roleEditCombo,
						loginDisabledEditCombo
					]
				},
				{
					columnWidth:.5,
					layout: 'form',  
					labelWidth:70,	 
					items: [
						{
							xtype:'textfield',
							fieldLabel: '用户名 ',
							name: 'truename',
							width:200,  
							allowBlank:false,
							blankText :'请输入用户名'
						}, 
						{
							xtype:'numberfield',
							fieldLabel: '电话',
							name: 'telephone',
							width:200,  
							allowBlank:false,
							blankText :'请输入电话号码'
						},
						{
							xtype:'textfield',
							fieldLabel: '地址',
							name: 'address',
							width:200, 	 
							allowBlank:false,
							blankText :'请输入地址信息'
						}
					]
				}
				]
		});		
	var UserEditWindow = function() {
		var _win = this;
		this.formPanel = userEditForm;
		var _form = this.formPanel.getForm();
		UserEditWindow.superclass.constructor.call(this, { 
			title: '用户信息',
			width : 650,
			height : 230,
			resizable : true,
			plain : false,
			border : false,
			modal : true,
			autoScroll : false,
			layout : 'fit',
			closeAction : 'hide',
			items : this.formPanel,
			buttonAlign:'center',
			buttons : 
			[
				{	text : ' 保存',
					handler : function() 
					{
						if (_form.isValid()) 
						{
							var form = _form.getValues(false);							
							UserBO = {};
							UserBO.id = form.id;	
							UserBO.username = form.username;	
							UserBO.rights = form.rights;	
							UserBO.truename = form.truename;	
							UserBO.identity = form.identity;	
							UserBO.telephone = form.telephone;	
							UserBO.address = form.address;	
							UserBO.logindisabled = form.logindisabled; 
							JSystemWeb.createUser(UserBO,function(data)
							{
								if(data)
	          					{
	          						_win.hide();
	          						Ext.Msg.alert('成功',"操作成功！",function()
	          						{
										ds.load({params:{start:0,limit:17}});
	          						}); 
									_form.reset(); 
	          					}else
	          					{
	          						Ext.Msg.alert("失败","操作失败!");
	          					}																
							})							
							
						}
					}
				},{
					text : '取消',
					handler : function() {
						_win.hide();
					},
					tooltip : '关闭对话框'
				}
			]	
		});
	}
	Ext.extend(UserEditWindow, Ext.Window); 
	var userEditWindow = new UserEditWindow();
	
	
	var PasswordkForm = function(){
			this.idTextField = {
					xtype : 'hidden',
					fieldLabel : "username",
					readOnly : true,
					name : "username"
				}; 			  
						
			this.passwordTextField = {
					xtype : 'textfield',
					inputType: 'password', 
					fieldLabel : "密码",
					allowBlank : false,
					name : "passwd",
					width:160,
					blankText :'请输入密码'	
			};	

			this.passwordTwoTextField = {
								xtype : 'textfield',
								inputType: 'password', 
								fieldLabel : "密码确认",
								allowBlank : false,
								name : "passwdTwo",
								width:160,
								blankText :'请输入确认密码'	
						};				
			
			PasswordkForm.superclass.constructor.call(this, {
					bodyStyle : 'padding:5px 5px 0',
					frame : true,
					id: 'PasswordkForm',
					labelAlign: 'right',
					labelWidth:100,
					items : [this.idTextField,  
							 this.passwordTextField,
							 this.passwordTwoTextField
							 ]
				});	
	}	
	Ext.extend(PasswordkForm, Ext.form.FormPanel);
	
	var PasswordWindow = function() {
		var _win = this;
		this.formPanel = new PasswordkForm();
		var _form = this.formPanel.getForm();
		PasswordWindow.superclass.constructor.call(this, {
			title : '修改密码',
			width : 300,
			height : 140,
			resizable : true,
			plain : false,
			border : false,
			modal : true,
			autoScroll : false,
			layout : 'fit',
			closeAction : 'hide',
			items : this.formPanel,
			buttonAlign:'center',
			buttons : 
			[
				{	text : ' 保存',
					handler : function() 
					{
						if (_form.isValid()) 
						{
							var form = _form.getValues(false); 	 	 	
							if(form.passwd != form.passwdTwo)
							{
								Ext.Msg.alert("提示","确认密码与第一次密码不一致!");
								return;
							} 
														
							//JSystemWeb.isAvialable(form.id,form.oldPassword,true,function(data)
							//{
								//if(data)
								//{
									JSystemWeb.updatePassword(form.username,form.passwdTwo,function(data)
									{
										if(data)
										{
											_win.hide();
											Ext.Msg.alert('成功',"操作成功！",function()
											{
												ds.load({params:{start:0,limit:17}});
											}); 
										}else
										{
											Ext.Msg.alert("失败","操作失败!");
										}																
									})
								//}else
									//Ext.Msg.alert("失败","原密码不对，请重新输入!");
							//})
							
							
							
						}
					}
				},{
					text : '取消',
					handler : function() {
						_win.hide();
					},
					tooltip : '关闭对话框'
				}
			]	
		});
	}
	Ext.extend(PasswordWindow, Ext.Window);
	var passwordWindow = new PasswordWindow();
	
})

function doDeleteFiberLink(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该用户?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	    	     	
			JSystemWeb.deleteUser(id, function(data) {
				 if(data) {							    	     		
					 ds.load({params:{start:0,limit:17}});
				 }else 
				 {
					Ext.Msg.alert("操作失败","允许用户登录操作失败!");
				 }
			 });			
		 }
		});
		
}

function doEnableUser(id)
{
	Ext.MessageBox.confirm('确认', '确认允许该用户登录?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	  
			JSystemWeb.EnableUser(id, function(data) {
				 if(data) {							    	     		
					 ds.load({params:{start:0,limit:17}});
				 }else 
				 {
					Ext.Msg.alert("操作失败","允许用户登录操作失败!");
				 }
			 });
			
		 }
	});
}

function doDisableUser(id)
{
	Ext.MessageBox.confirm('确认', '确认不允许该用户登录?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {   	
			JSystemWeb.disableUser(id, function(data) {
				 if(data) {							    	     		
					 ds.load({params:{start:0,limit:17}});
				 }else 
				 {
					Ext.Msg.alert("操作失败","不允许用户登录操作失败!");
				 }
			 });
			
		 }
	});
}

</script>
	
</body>
</html>